//
//  CGRetrieveViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/3.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGRetrieveViewController.h"
#import "CGResetPasswordViewController.h"
#import "CGAccountAppealViewController.h"

@interface CGRetrieveViewController ()<UITextFieldDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UITextField *accountText;            //账号
@property(nonatomic,strong)UITextField *phoneText;              //手机号
@property(nonatomic,strong)UIButton *fineButton;                //立即找回

@property(nonatomic,assign)CGFloat textFontSize;

@end

@implementation CGRetrieveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"找回密码";
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    
    self.textFontSize = 15;
    
    [self layoutUI];
}

-(void)layoutUI{
    
    //底部添加一个scrollowView {{0, 0}, {320, 480}}
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 253)];
    
    //self.scrollView.backgroundColor = [UIColor yellowColor];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 227);
    [self.view addSubview:self.scrollView];
    
    CGRect aFrame = CGRectMake(20, 30, SCREEN_WIDTH - 20 - 20, 44);
    CGRect bFrame = CGRectMake(60, 35, SCREEN_WIDTH - 40 - 40, 34);
    CGRect cFrame = CGRectMake(30, 42, 20, 20*38/37);
    //账号
    UIImageView *accountView = [[UIImageView alloc]initWithFrame:aFrame];
    accountView.image = [UIImage imageNamed:@"login_input_phone_bg"];
    [self.scrollView addSubview:accountView];
    //输入文本框
    self.accountText = [[UITextField alloc]initWithFrame:bFrame];
    self.accountText.clearButtonMode = UITextFieldViewModeAlways;
    self.accountText.placeholder = @"请输入您的账号";
    self.accountText.font = [UIFont systemFontOfSize:self.textFontSize];
    [self.accountText becomeFirstResponder];
    [self.scrollView addSubview:self.accountText];
    self.accountText.delegate = self;
    //图标
    UIImageView *accountImageView = [[UIImageView alloc]initWithFrame:cFrame];
    accountImageView.image = [UIImage imageNamed:@"login_phone_icon"];
    [self.scrollView addSubview:accountImageView];
    
    //手机号码
    aFrame.origin.y += 60;
    bFrame.origin.y += 60;
    cFrame.origin.y += 60;
    UIImageView *passwordView = [[UIImageView alloc]initWithFrame:aFrame];
    passwordView.image = [UIImage imageNamed:@"login_input_phone_bg"];
    [self.scrollView addSubview:passwordView];
    //输入文本框
    self.phoneText = [[UITextField alloc]initWithFrame:bFrame];
    self.phoneText.clearButtonMode = UITextFieldViewModeAlways;
    self.phoneText.placeholder = @"请输入账号对应的手机号码";
    self.phoneText.font = [UIFont systemFontOfSize:self.textFontSize];
    [self.scrollView addSubview:self.phoneText];
    self.phoneText.delegate = self;
    //图标
    //cFrame.size.width = 20*35/42;
    UIImageView *passwordImageView = [[UIImageView alloc]initWithFrame:cFrame];
    passwordImageView.image = [UIImage imageNamed:@"login_password_icon"];
    [self.scrollView addSubview:passwordImageView];
    
    //立即找回Button
    aFrame.origin.y += 80;
    self.fineButton = [[UIButton alloc]initWithFrame:aFrame];
    [self.fineButton setBackgroundImage:[UIImage imageNamed:@"login_btn_bg"] forState:UIControlStateNormal];
    [self.fineButton setTitle:@"立即找回" forState:UIControlStateNormal];
    self.fineButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.scrollView addSubview:self.fineButton];
    //添加点击事件
    [self.fineButton addTarget:self action:@selector(fineButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    //添加输入框键盘收回事件
    [self.accountText addTarget:self action:@selector(nextTextField) forControlEvents:UIControlEventEditingDidEndOnExit];
    
    [self.phoneText addTarget:self action:@selector(phoneDone) forControlEvents:UIControlEventEditingDidEndOnExit];
    
}

#pragma mark 键盘弹出/退回事件
-(void)nextTextField{
    
    [self.phoneText becomeFirstResponder];
}

-(void)phoneDone{
    
    [self.phoneText resignFirstResponder];
}



#pragma mark 点击立即找回按钮
-(void)fineButtonClick{
    
    //1.有效性验证,去除字符串两端的空格
    NSString *account = [self.accountText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *phone = [self.phoneText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (account.length == 0) {
        [Tools alertWithTitle:@"温馨提示" andMessage:@"请输入账号"];
        return;
    }else if (![Tools isMobileNumber:phone]){
        [Tools alertWithTitle:@"温馨提示" andMessage:@"请输入正确的手机号码"];
        return;
    }
    
    
    //发送请求
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"account"] = self.accountText.text;
    parameters[@"tphone"] = self.phoneText.text;
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/main/updateResetPassword.app" parameters:parameters success:^(id responseData) {
        
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];

        if ([result rangeOfString:@"用户不存在"].location != NSNotFound) {
            
            [Tools alertWithTitle:@"温馨提示" andMessage:@"用户不存在或手机号码不匹配"];
            return;
            
        }else if ([result rangeOfString:@"account"].location != NSNotFound){
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
    } failure:^(NSError *error) {
        
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
        return;
    }];
    
}

#pragma mark 注册键盘通知
-(void)viewWillAppear:(BOOL)animated{
    /*
     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 253}}";
     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 227}, {320, 253}}";
     */
    [super viewWillAppear:animated];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(openKeyboard:) name:UIKeyboardDidShowNotification object:nil];
    [center addObserver:self selector:@selector(closeKeyboard:) name:UIKeyboardWillHideNotification object:nil];
    
}

//键盘弹起时
-(void)openKeyboard:(NSNotification *)notification{
    
    self.scrollView.contentSize =  CGSizeMake(SCREEN_WIDTH, 291);
    
}

//键盘收起时
-(void)closeKeyboard:(NSNotification*)notification{
    
    self.scrollView.contentSize =  CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 253);
}



@end
